﻿Imports System.Threading

Public Class FmHook
    Private hook As New vbnet_hook
    ' Private WithEvents evenerhook As vbnet_hook
    Private Sub FmHook_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '键盘事件。
        AddHandler hook.KeyDown, AddressOf Keyboardhook_keyDoun
        'AddHandler hook.KeyUp, AddressOf keyboardhook_keyUp
        AddHandler hook.KeyPress, AddressOf keyboardhook_keypree
        hook.Start(True, False)
        AddHandler checkBox1.CheckedChanged, AddressOf checkBox1_CheckedChanged
    End Sub

    Private Sub checkBox1_CheckedChanged(sender As Object, e As EventArgs)
        If checkBox1.Checked Then
            AddHandler hook.MouseMove, AddressOf mouseHook_MouseMove
            AddHandler hook.MouseDown, AddressOf mouseHook_MouseDown
            AddHandler hook.MouseUp, AddressOf mouseHook_MouseUp
            hook.Start(False, True)
        Else
            AddHandler hook.MouseMove, AddressOf mouseHook_MouseMove

            AddHandler hook.MouseDown, AddressOf mouseHook_MouseDown

            AddHandler hook.MouseUp, AddressOf mouseHook_MouseUp
            hook.UnHook(False, False)
        End If
    End Sub


    Private isDown As Boolean = False

    Private Sub mouseHook_MouseDown(sender As Object, e As MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            '鼠标左键点下 则开始绘制
            isDown = True
        End If

    End Sub
    Private isMove As Boolean = False

    Private Sub mouseHook_MouseMove(sender As Object, e As MouseEventArgs)
        RichTextBox2.Text = Cursor.Current.ToString() + e.Location.ToString

        If isDown AndAlso checkBox1.Checked Then
            isMove = True
        End If
    End Sub

    Private Sub mouseHook_MouseUp(sender As Object, e As MouseEventArgs)

        If e.Button = MouseButtons.Left Then
            Dim t As New Thread(Sub()
                                    'Thread.Sleep(2000);
                                    If isDown AndAlso isMove Then
                                        Clipboard.Clear()
                                        '发送ctrl+c键，复制鼠标选中的文字。
                                        SendKeys.SendWait("^c")
                                        '使用 Flush 等待应用程序处理键盘敲击以及消息队列中的其他操作系统消息。
                                        SendKeys.Flush()
                                        Dim iData As IDataObject = Clipboard.GetDataObject()
                                        If iData IsNot Nothing Then
                                            If iData.GetDataPresent(DataFormats.Text) Then
                                                '检查是否存在文本
                                                '  For i As Integer = 0 To 5
                                                Thread.Sleep(200)
                                                '  Dim res As String = DirectCast(iData.GetData(DataFormats.Text), [String])
                                                Dim res As String = CType(iData.GetData(DataFormats.Text, True), String)
                                                ' If Not String.IsNullOrWhiteSpace(res) Then'.net3.5里面没有IsNullOrWhiteSpace
                                                If Not String.IsNullOrEmpty(res) Then
                                                    RichTextBox1.Text = res
                                                End If
                                                ' Next
                                                '从剪贴板中移除所有数据。

                                            End If

                                        End If
                                    End If
                                    isDown = False
                                    isMove = False

                                End Sub)
            Control.CheckForIllegalCrossThreadCalls = False
            t.SetApartmentState(ApartmentState.STA)

            t.Start()
        End If
    End Sub



    Private Sub FmHook_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        hook.UnHook(False, False)
    End Sub
#Region "键盘事件"
    '这是委托的键盘事件。
    Private Sub Keyboardhook_keyDoun(sender As Object, e As KeyEventArgs)
        '键盘钩子的使用。
        Label1.Text = e.KeyCode.ToString + "--"
    End Sub

    '不用委托的键盘事件。
    Private Sub keyboardhook_keyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs)
        Label1.Text += e.ToString + "--"
    End Sub

    Private Sub keyboardhook_keypree(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
        Label1.Text += e.KeyChar.ToString
    End Sub
#End Region

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        'Dim tiptext As String = String.Format("({0},{1})", e.X, e.Y) '格式化在气泡上要显示的x，y坐标
        'tooltip.Show(tiptext, Me, e.Location)
    End Sub

End Class
